// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   Logger.java

package com.vandagroup.common.util;

import java.io.*;
import java.sql.Timestamp;

public class Logger
{

    public String date;
    public String TRANS_LOG_FILE_NAME;
    public String LOG_FILE_NAME;
    public boolean transOnDate;
    public boolean logOnDate;
    public PrintWriter logWriter;
    public PrintWriter transWriter;
    public static Logger instance = null;

    protected Logger()
    {
        date = null;
        TRANS_LOG_FILE_NAME = "TransLog_";
        LOG_FILE_NAME = "server";
        transOnDate = true;
        logOnDate = false;
        logWriter = null;
        transWriter = null;
    }

    public static Logger getInstance()
    {
        if(instance == null)
            instance = new Logger();
        instance.init();
        return instance;
    }

    public static Logger getInstance(String transLogFile, String logFile, boolean transOnDate, boolean logOnDate)
    {
        if(instance == null)
            instance = new Logger();
        instance.TRANS_LOG_FILE_NAME = transLogFile;
        instance.LOG_FILE_NAME = logFile;
        instance.transOnDate = transOnDate;
        instance.logOnDate = logOnDate;
        instance.init();
        return instance;
    }

    private void init()
    {
        String ts = (new Timestamp(System.currentTimeMillis())).toString();
        initLogWriter(ts);
        initTransWriter(ts);
    }

    public void log(String msg)
    {
        String ts = (new Timestamp(System.currentTimeMillis())).toString();
        initLogWriter(ts);
        if(logWriter != null)
        {
            logWriter.print("[" + ts + "]");
            logWriter.println(msg);
            logWriter.flush();
        }
    }

    public void log(String msg, Throwable exp)
    {
        log(msg);
        if(logWriter != null)
            exp.printStackTrace(logWriter);
        exp.printStackTrace(System.out);
    }

    public void logTrans(String msg)
    {
        String ts = (new Timestamp(System.currentTimeMillis())).toString();
        initTransWriter(ts);
        if(transWriter != null)
        {
            transWriter.print("[" + ts + "]");
            transWriter.println(msg);
            transWriter.flush();
        }
        System.out.println(msg);
    }

    public void logTrans(String msg, Throwable exp)
    {
        logTrans(msg);
        if(transWriter != null)
            exp.printStackTrace(transWriter);
        exp.printStackTrace();
    }

    private void initLogWriter(String ts)
    {
        if(logWriter == null || logOnDate && !ts.substring(0, 10).equals(date))
        {
            String fileName = null;
            if(logOnDate)
            {
                date = ts.substring(0, 10);
                fileName = LOG_FILE_NAME + date.replace('-', '_') + ".log";
            } else
            {
                fileName = LOG_FILE_NAME + ".log";
            }
            try
            {
                logWriter = new PrintWriter(new FileWriter(fileName, true));
            }
            catch(IOException ex)
            {
                logWriter = null;
            }
        }
    }

    private void initTransWriter(String ts)
    {
        if(transWriter == null || transOnDate && !ts.substring(0, 10).equals(date))
        {
            String fileName = null;
            if(transOnDate)
            {
                date = ts.substring(0, 10);
                fileName = TRANS_LOG_FILE_NAME + date.replace('-', '_') + ".log";
            } else
            {
                fileName = TRANS_LOG_FILE_NAME + ".log";
            }
            try
            {
                transWriter = new PrintWriter(new FileWriter(fileName, true));
            }
            catch(IOException ex)
            {
                transWriter = null;
            }
        }
    }

}
